package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.commons.utils.UserHolder;
import com.tanhua.domain.UserInfo;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserControler {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据id查询用户
     *
     * @param userId
     * @param token
     * @return
     */
    @GetMapping()
    public ResponseEntity<UserInfoVo> users(Long userId, @RequestHeader("Authorization") String token) {
        //判断userId是否为空
        if (userId == null) {
            userId = UserHolder.getUserId();
        }
        //为用户设置信息
        UserInfoVo vo = userInfoService.findById(userId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        //为userInfo获取id
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
}
